/******************************************************************************
 * Function for config files reading
 * Developed by Carlos Alberto Ynoguti
 *
 * Usage: 
 *
 * 
 * 
 * The organization of the config.cfg is described in the file CONFIGURE.TXT
 ******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "estruturas.h"	
#include "loadConfig.h"

int loadConfig
(
  	char *configFile,
	struct configBusca *searchConfig,
	char *grammarFile, // file with the language model
	char *HMMFile // file with the HMM parameters
)
{
  // Local variables
  FILE *ptr; // file handlers
  char data[512],aux[512];
	
	// Opening configuration file
  	ptr = fopen(configFile,"rt");
	
	if (ptr == NULL)
	{
		printf("Error opening file %s.\n",configFile);
		return 1;
	}
	
  	// Reading informations
  	fgets(data, 500, ptr);
  
  	// removing control characters (\r \n) frm the end of string
  	while (data[strlen(data)-1]=='\r' || data[strlen(data)-1]=='\n')
		data[strlen(data)-1] = '\0';

  	while (!feof(ptr))
  	{
		// Beam Search
	    if (strstr(data,"Beam search=") != NULL)
		{
		  	sscanf(data,"Beam search=%s",aux);
			if (strcmp(aux,"sim") == 0)
		    	searchConfig->useBeamSearch = 1;
		  	else 
	  	  		searchConfig->useBeamSearch = 0;
		}
		// Beam Search threshold
	    if (strstr(data,"Limiar de poda=") != NULL)
		  	sscanf(data,"Limiar de poda=%f",&searchConfig->beamSearchThreshold);
		// Number of levels
	    if (strstr(data,"Numero de niveis=") != NULL)
		  	sscanf(data,"Numero de niveis=%d",&searchConfig->numberOfLevels);
		// Word duration model
	    if (strstr(data,"Modelo de duracao=") != NULL)
		{
		  	sscanf(data,"Modelo de duracao=%s",aux);
			if (strcmp(aux,"sim") == 0)
		    	searchConfig->useWordDurationModel = 1;
		  	else 
	  	  	searchConfig->useWordDurationModel = 0;
	    }
		// Grammar
	    if (strstr(data,"Usa gramatica=") != NULL)
		{
		  	sscanf(data,"Usa gramatica=%s",aux);
			if (strcmp(aux,"sim") == 0)
		    	searchConfig->useGrammar = 1;
		  	else 
	  	  		searchConfig->useGrammar = 0;
		}
		// Grammar File
	    if (strstr(data,"Gramatica=") != NULL)
		  	sscanf(data,"Gramatica=%s",grammarFile);

		// HMM File
	    if (strstr(data,"Modelos HMM=") != NULL)
		  	sscanf(data,"Modelos HMM=%s",HMMFile);
			
		// Reading new entry from file
		if (!feof(ptr))
			fgets(data, 500, ptr);
			
	  	// removing control characters (\r \n) frm the end of string
		while (data[strlen(data)-1]=='\r' || data[strlen(data)-1]=='\n')
			data[strlen(data)-1] = '\0';
		

  	} // end of configuration file reading

  	fclose(ptr);
	
  	return (0);
}
